SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
### 读取版本变量
######################################################################################
#   CMAKE_CURRENT_SOURCE_DIR 在这里为项目根目录，必须含有版本信息的version文件       #
#                                                可以获取GIT版本信息                 #
######################################################################################
EXECUTE_PROCESS(COMMAND 
    git rev-parse --short HEAD  
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_REVISION)
STRING(STRIP  ${GIT_REVISION} GIT_REVISION)
#string(REGEX REPLACE "^(\\w\\w).*$" "\\1" GIT_REVISION2  ${GIT_REVISION})
#string(SUBSTRING ${GIT_REVISION} 0 7 GIT_REVISION2)

FIND_FILE(BRANCH_FILE branch PATHS ${CMAKE_BINARY_DIR} NO_DEFAULT_PATH) 

if (BRANCH_FILE)
    FILE(READ ${BRANCH_FILE} GIT_BRANCH)
else()
    EXECUTE_PROCESS(COMMAND 
        git rev-parse --abbrev-ref HEAD
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE GIT_BRANCH) 
endif()
STRING(STRIP  ${GIT_BRANCH} GIT_BRANCH)
    
EXECUTE_PROCESS(COMMAND 
    date +%Y%m%d%H%M%S
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    OUTPUT_VARIABLE DATE)
STRING(STRIP  ${DATE} DATE) 
    
FILE(READ ${CMAKE_CURRENT_SOURCE_DIR}/version VER)
    
SET(VER_STRING "${VER}.${GIT_REVISION}.${DATE} ${GIT_BRANCH}")

##生成version文件
function(gen_version_file FILE_NAME)
    set(_options APPEND_PATH DESCRIPTORS)
    set(_singleargs TARGET  OUT_DIR)
    set(_multiargs OPTS  )
		
	cmake_parse_arguments(gen_ver "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
	
    if (gen_ver_OUT_DIR)    
        SET(OUT_FLIE ${gen_ver_OUT_DIR}/${FILE_NAME})
    else()
        SET(OUT_FLIE ${FILE_NAME})
    endif()
 
    
    FILE(WRITE ${OUT_FLIE}  "${VER_STRING}")
    #FILE(APPEND  ${OUT_FLIE}  "const VER = \"${VER}\"\n" )
    #FILE(APPEND  ${OUT_FLIE}  "const VER_REVISION = \"${GIT_REVISION}\"\n")
    #FILE(APPEND  ${OUT_FLIE}  "const DATE = \"${DATE}\"\n")
    #FILE(APPEND  ${OUT_FLIE}  "const GIT_BRANCH = \"${GIT_BRANCH}\"\n")
    #FILE(APPEND  ${OUT_FLIE}  "const VER_STRING = \"${VER_STRING}\"\n" )

endfunction()


##生成 Version.go文件,  不建议使用该函数， 因为会修改代码，是否重新提交是一个问题
##实现操作可以通过  go build -ldflags "-X main.VERSION=1.0.0 -X 'main.BUILD_TIME=`date`' -X 'main.GO_VERSION=`go version`'" 传递变量
function(gen_go_version_file FILE_NAME)
    set(_options APPEND_PATH DESCRIPTORS)
    set(_singleargs TARGET  OUT_DIR)
    set(_multiargs OPTS  )
		
	cmake_parse_arguments(gen_ver "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
	
    if (gen_ver_OUT_DIR)    
        SET(OUT_FLIE ${gen_ver_OUT_DIR}/${FILE_NAME})
    else()
        SET(OUT_FLIE ${FILE_NAME})
    endif()
 
    
    FILE(WRITE ${OUT_FLIE}  "package main\n")
    FILE(APPEND  ${OUT_FLIE}  "const VER = \"${VER}\"\n" )
    FILE(APPEND  ${OUT_FLIE}  "const VER_REVISION = \"${GIT_REVISION}\"\n")
    FILE(APPEND  ${OUT_FLIE}  "const DATE = \"${DATE}\"\n")
    FILE(APPEND  ${OUT_FLIE}  "const GIT_BRANCH = \"${GIT_BRANCH}\"\n")
    FILE(APPEND  ${OUT_FLIE}  "const VER_STRING = \"${VER_STRING}\"\n" )

endfunction()